package spring.cloud.tasks.api.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spring.cloud.tasks.api.domain.zooKeeperClusterAndNameSpace.ZooKeeperCluster;
import spring.cloud.tasks.api.service.ZooKeeperClusterService;
import spring.cloud.tasks.api.utils.Response;

import java.util.LinkedList;
import java.util.List;

@RequestMapping("/zooKeeperCluster")
public class ZooKeeperClusterController {

    @Autowired
    private ZooKeeperClusterService zooKeeperService;

    /**
     * 刷新注册中心
     */

    @RequestMapping(value = "/notifyRegistryCenterRefresh", method = {RequestMethod.GET, RequestMethod.POST})
    public Response notifyRefresh() throws RuntimeException {
        zooKeeperService.notifyRegistryCenterRefresh();
        return new Response();
    }

    //@ApiResponses(value = {@ApiResponse(code = 200, message = "Success/Fail", response = RequestResult.class)})
    //@Audit
    @PostMapping(value = "/addZooKeeperCluster")
    public Response addZooKeeperCluster(@RequestParam String zkClusterKey,
                                        @RequestParam String zooKeeperAddressList,
                                        @RequestParam String digest,
                                        @RequestParam(required = false) String description)
            throws RuntimeException {

        zooKeeperService.addZooKeeperCluster(zkClusterKey, zooKeeperAddressList, digest, description);
        return new Response();
    }


    @PutMapping(value = "/updateZooKeeperCluster")
    public Response updateZkCluster(@RequestParam String zooKeeperClusterKey,
                                    @RequestParam String connectString,
                                    @RequestParam(required = false) String description)
            throws RuntimeException {
        //assertIsPermitted(PermissionKeys.registryCenterAddZkCluster);
        zooKeeperService.updateZooKeeperCluster(zooKeeperClusterKey, connectString, description);
        return new Response();
    }


    @PutMapping(value = "/deleteZooKeeperCluster")
    public Response deleteZooKeeperCluster(@RequestParam String zkClusterKey)
            throws RuntimeException {

        zooKeeperService.deleteZooKeeperCluster(zkClusterKey);
        return new Response();
    }

    /**
     * 获取单个或所有zk集群信息
     */

    @GetMapping(value = "/getZooKeeperCluster")
    public Response getZooKeeperCluster(@RequestParam(required = false) String status,
                                        @RequestParam(required = false) String zooKeeperClusterKey) throws RuntimeException {
        if (StringUtils.isNotBlank(zooKeeperClusterKey)) {
            ZooKeeperCluster zooKeeperCluster = zooKeeperService.getZooKeeperCluster(zooKeeperClusterKey);
            if (zooKeeperCluster == null) {
                throw new RuntimeException(String.format("ZooKeeperCluster[%s]不存在", zooKeeperClusterKey));
            }
            return Response.success(zooKeeperCluster);
        }

        List<ZooKeeperCluster> zooKeeperClusterList = zooKeeperService.getZooKeeperClusterList();
        if (StringUtils.isBlank(status) || !"online".equals(status)) {
            return Response.success(zooKeeperClusterList);
        }

        List<ZooKeeperCluster> zooKeeperClusters = filterOnlineZkClusters(zooKeeperClusterList);
        return Response.success(zooKeeperClusters);
    }

    private List<ZooKeeperCluster> filterOnlineZkClusters(List<ZooKeeperCluster> zooKeeperClusterList) {
        if (zooKeeperClusterList == null) {
            return new LinkedList<ZooKeeperCluster>();
        }

        List<ZooKeeperCluster> list = new LinkedList<ZooKeeperCluster>();
        for (ZooKeeperCluster zooKeeperCluster : zooKeeperClusterList) {
            if (!zooKeeperCluster.isOffline()) {
                list.add(zooKeeperCluster);
            }
        }
        return list;
    }
    //


}
